package com.yq.service;

import com.yq.bean.ClockWork;
import com.yq.bean.Clock_info;
import com.yq.bean.Employee;
import com.yq.bean.Notification;
import com.yq.dao.ClockDao;
import com.yq.dao.EmployeeDao;
import com.yq.util.JdbcTool;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class EmployeeService {
    //创建dao对象
    EmployeeDao employeeDao = new EmployeeDao();
    ClockDao clockDao = new ClockDao();

    //登录
    public Notification loginEmployee(Notification notification) {
        //提供数据库链接
        Connection con = JdbcTool.getConnection();
        //获取用户名和密码
        String loginName = (String) notification.getMap().get("loginName");
        String pw = (String) notification.getMap().get("pw");

        try {
            //调用dao的登录方法
            Employee employee = employeeDao.login_Employee(con, loginName, pw);
            if (employee != null) {
                notification.setFlag(true);
                notification.getMap().put("nowEmployee", employee);
                notification.getMap().put("employee_id", employee.getEmployee_id());
                notification.getMap().put("employee_name", employee.getEmployee_name());
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "账户或密码不正确，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //根据e_id获取role
    public Notification getRole(Notification notification) {
        Connection con = JdbcTool.getConnection();
        Integer e_id = (Integer) notification.getMap().get("e_id");
        Integer role = null;
        try {
            role = employeeDao.get_Role(con, e_id);
            if (role >= 0) {
                notification.setFlag(true);
                notification.getMap().put("role", role);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "身份有错误");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //根据e_id获取e_no
    public Notification getNo(Notification notification) {
        Connection con = JdbcTool.getConnection();
        Integer e_id = (Integer) notification.getMap().get("e_id");
        String no = null;
        try {
            no = employeeDao.get_NO(con, e_id);
            if (no != null) {
                notification.setFlag(true);
                notification.getMap().put("e_no", no);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "获取no失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //注册
    public Notification registerEmployee(Notification notification) {
        Connection con = JdbcTool.getConnection();
        Employee newEmployee = (Employee) notification.getMap().get("newEmployee");

        try {
            boolean register_employee = employeeDao.Register_Employee(con, newEmployee);
            if (register_employee) {
                notification.setFlag(true);
                notification.getMap().put("newEmployee", newEmployee);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "注册失败，请重新操作！");

            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return notification;
    }

    //上班打卡
    public Notification in_time_Clock(Notification notification) {
        Connection con = JdbcTool.getConnection();
        Clock_info in_clock = (Clock_info) notification.getMap().get("in_clock");
        boolean in_clock_ok = false;
        try {
            //判断1：如果不是工作日，则不能打卡
            if (!clockDao.check_clock_date(con)) {
                notification.getMap().put("message", "上班打卡失败，今天不是工作日~");
                return notification;
            }
            //判断2：超过12点不能打卡上班
            if (clockDao.time(con)==0){
                notification.getMap().put("message", "上班打卡失败，超过中午12点不能打卡上班");
                return notification;
            }
            //判断3：如果打过上班卡，就不能打了
            if (clockDao.check_clock(con, in_clock)) {
                notification.getMap().put("message", "上班打卡失败，已经打过卡了~");
                return notification;
            }
            //打卡
            in_clock_ok = clockDao.in_clock(con, in_clock);
            if (in_clock_ok) {
                notification.setFlag(true);
                notification.getMap().put("in_clock", in_clock);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "上班打卡失败，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //下班打卡
    public Notification off_time_Clock(Notification notification) {
        Connection con = JdbcTool.getConnection();
        Clock_info ci = (Clock_info) notification.getMap().get("off_clock");
        boolean off_clock_ok = false;

        try {
            //判断1：如果不是工作日，则不能打卡
            if (!clockDao.check_clock_date(con)) {
                notification.getMap().put("message", "下班打卡失败，今天不是工作日~");
                return notification;
            }
            //判断2：不到12点不能打卡下班
            if (clockDao.time(con)==1){
                notification.getMap().put("message", "下班打卡失败，不到中午12点，不能打卡下班");
                return notification;
            }
            //判断3：如果打过下班卡，就不能打了
            if (clockDao.check_clock2(con, ci)) {
                notification.getMap().put("message", "下班打卡失败，已经打过卡了~");
                return notification;
            }

            if (clockDao.check_clock(con, ci)) {
                //如果打卡了,update
                off_clock_ok = clockDao.off_clock(con, ci);
            }//如果没打卡,insert
            else {
                off_clock_ok = clockDao.off_clock2(con, ci);
            }
            //如果打卡成功
            if (off_clock_ok) {
                notification.setFlag(true);
                notification.getMap().put("off_clock", ci);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "下班打卡失败，请重新操作！");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //查看个人考勤信息
    public Notification selectClock(Notification notification) {
        Connection con = JdbcTool.getConnection();
        String e_no = (String) notification.getMap().get("e_no");
//        System.out.println(e_no);
        try {
            List<ClockWork> clockWorks = clockDao.selectClock(con, e_no);
            if (clockWorks.size() > 0) {
                notification.setFlag(true);
                notification.getMap().put("clockWorks", clockWorks);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "没有数据，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //查看个人考勤信息（模糊搜索）部分
   /* public Notification selectClockNo(Notification notification) {
        Connection con = JdbcTool.getConnection();
        String e_no = (String) notification.getMap().get("e_no");
        try {
            List<ClockWork> clockWorks = clockDao.selectClockNo(con, e_no);
            if (clockWorks.size()>0) {
                notification.setFlag(true);
                notification.getMap().put("clockWorks", clockWorks);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "没有数据，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //查看个人考勤信息（模糊搜索）
    public Notification selectClockName(Notification notification) {
        Connection con = JdbcTool.getConnection();
        String e_no = (String) notification.getMap().get("e_name");
        try {
            List<ClockWork> clockWorks = clockDao.selectClockName(con, e_no);
            if (clockWorks.size()>0) {
                notification.setFlag(true);
                notification.getMap().put("clockWorks", clockWorks);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "没有数据，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }
    //查看个人考勤信息（模糊搜索）
    public Notification selectClockDate(Notification notification) {
        Connection con = JdbcTool.getConnection();
        String w_date = (String) notification.getMap().get("w_date");
        try {
            List<ClockWork> clockWorks = clockDao.selectClockDate(con, w_date);
            if (clockWorks.size()>0) {
                notification.setFlag(true);
                notification.getMap().put("clockWorks", clockWorks);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "没有数据，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }*/

    //查看个人考勤信息（模糊搜索）
    public Notification selectClockAll(Notification notification) {
        Connection con = JdbcTool.getConnection();
        String w_date = (String) notification.getMap().get("w_date");
        String e_name = (String) notification.getMap().get("e_name");
        String e_no = (String) notification.getMap().get("e_no");
        try {
            List<ClockWork> clockWorks = clockDao.selectClockAll(con, e_no, e_name, w_date);
            if (clockWorks.size() > 0) {
                notification.setFlag(true);
                notification.getMap().put("clockWorks", clockWorks);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "没有数据，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }

    //查看所有考勤信息
   /* public Notification selectClockAll(Notification notification) {
        Connection con = JdbcTool.getConnection();
        try {
            List<ClockWork> clockWorks = clockDao.selectClockAll(con);
            if (clockWorks.size()>0) {
                notification.setFlag(true);
                notification.getMap().put("clockWorks", clockWorks);
            } else {
                notification.setFlag(false);
                notification.getMap().put("message", "没有数据，查询失败");
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return notification;
    }*/
}
